﻿Public Class ccapNetTopologyForm

    '电话号码对应起始地址

    Private DevCount_offset As UShort = 2005


    '正在修改参数标志
    Private _isModify As Boolean = False

    Private Sub ccapNetTopologyForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.TimerHeart.Stop()
        MyBase.OnClosing(e)
    End Sub

    Private Sub ccapNetTopologyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SetModifyState(False)
        Me.btnModify.Enabled = False
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        LoadData()
    End Sub

    ''' <summary>
    ''' 读取当前电话号码
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadData()
        Dim buf() As Short
        If g_test Then
            ReDim buf(1)
            Dim r As New Random
            buf(0) = r.Next(1, 10)
        Else
            buf = COMHelper.Instance.Send4Short(g_netID, FunctionCode.WordWritableRead, DevCount_offset, 1)
        End If

        If buf Is Nothing Then
            Me.btnModify.Enabled = False
            msg.ShowError(g_strReadError)
            Exit Sub
        End If

        Me.btnModify.Enabled = True
        nud_devcount.Value = buf(0)
        nud_devcount.Tag = buf(0)

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.TimerHeart.Stop()
        Me.Close()
    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        Me.btnModify.Enabled = False
        If EnterModify() = False Then
            'msg.Show("其它地方正在修改参数,请稍后再试.")
            Me.btnModify.Enabled = True
            Exit Sub
        End If

        SetModifyState(True)
    End Sub

    Private Sub SetModifyState(ByVal value As Boolean)
        Me.btnModify.Enabled = Not value
        Me.btnSave.Enabled = value

        Me.nud_devcount.ReadOnly = Not value
        Me.nud_devcount.Increment = IIf(value, 1, 0)
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If CheckValida() = False Then
            Exit Sub
        End If

        Dim result As Boolean
        Me.Cursor = Cursors.WaitCursor
        _isSaving = True
        result = Save()
        _isSaving = False
        Me.Cursor = Cursors.Default
        If result Then
            SetModifyState(False)
            msg.Show("保存成功")
        Else
            msg.ShowError("保存失败")
        End If
    End Sub

    ''' <summary>
    ''' 判断用户输入数据的合法性
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckValida() As Boolean
        If Me.nud_devcount.Value > 9 Then
            msg.ShowWarning("超过最大设备数量")
            Me.nud_devcount.Focus()
            Return False
        End If

        Return True
    End Function

    ''' <summary>
    ''' 保存所有电话号码
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Save() As Boolean
        If Me.nud_devcount.Value <> Me.nud_devcount.Tag Then
            Dim result As Boolean
            result = Save(DevCount_offset, Convert.ToUInt16(Me.nud_devcount.Value))
            If result = False Then
                Return False
            End If
        End If

        '设置修改使能变量
        If ParameterModifyClass.SetModifyConfirm(g_netID) = False Then
            Return False
        End If

        ' Me.TimerHeart.Stop()

        '修改门变量
        If ExitModify() = False Then
            Debug.Write("ExitModify错误")
        End If

        Return True
    End Function

    ''' <summary>
    ''' 保存参数
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function save(ByVal index As UShort, ByVal value As Short) As Boolean
        Dim offset As UShort = index + 1000
        Return COMHelper.Instance.Write(g_netID, FunctionCode.WordWrite, offset, value)
    End Function


    ''' <summary>
    ''' 进入修改
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function EnterModify() As Boolean
        If g_test Then
            _isModify = True
            Return True
        End If


        Dim result As Boolean
        result = ParameterModifyClass.EnterModify(g_netID)

        If result = False Then
            Return False
        End If

        '开始发送心跳包
        Me.TimerHeart.Start()

        _isModify = True

        Return True
    End Function


    ''' <summary>
    ''' 退出修改
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ExitModify() As Boolean
        '门变量置为1
        Dim result As Boolean
        result = ParameterModifyClass.ExitModify(g_netID)

        '停止发送心跳包
        Me.TimerHeart.Stop()

        _isModify = False

        Return True
    End Function

#Region "发送心跳包"

    '心跳包连续发送错误的次数
    Private _heartFailCount As Integer = 0

    '心跳值
    Private _heartValue As Short = 0

    '心跳值是否初始化
    Private _bheartValue As Boolean = False

    '是否正在发送心跳标志
    Private _heartSending As Boolean = False

    '是否正在保存标志
    Public Shared _isSaving As Boolean = False

    '心跳包地址
    Private _heartIncreamentAddress As UShort = 7063

    ''' <summary>
    ''' 发送心跳包
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub HeartSend()
        If _bheartValue = False Then
            Dim b() As Short
            b = COMHelper.Instance.Send4Short(g_netID, FunctionCode.WordWritableRead, _heartIncreamentAddress, 1)
            If b IsNot Nothing AndAlso b.Length = 1 Then
                _heartValue = b(0)
                _bheartValue = True
            Else
                Exit Sub
            End If
        End If

        _heartValue = (_heartValue + 1) Mod UShort.MaxValue

        Dim result As Boolean
        result = COMHelper.Instance.Write(g_netID, FunctionCode.WordWrite, _heartIncreamentAddress, _heartValue)

        If result Then
            _heartFailCount = 0
        Else
            _heartFailCount += 1
        End If

        If _heartFailCount > 10 Then
            '提示用户可能断开连接了.
        End If

    End Sub

    ''' <summary>
    ''' 心跳定时器Tick事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TimerHeart_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerHeart.Tick
        If Not g_HeartSend Then
            Exit Sub
        End If

        If _heartSending OrElse _isSaving Then
            Exit Sub
        End If

        _heartSending = True
        Me.HeartSend()
        _heartSending = False
    End Sub

#End Region
End Class